<template>
  <div class="container">
    <!-- 筛选栏 -->
    <div class="filter-container">
      <!-- 筛选条 -->
      <div class="filter-item">
        <span class="filter-label">地区：</span>
        <el-radio-group v-model="selectedRegion" @change="handleFilter">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button
            v-for="region in filterOptions.regions"
            :key="region"
            :label="region"
          />
        </el-radio-group>
      </div>

      <div class="filter-item">
        <span class="filter-label">类型：</span>
        <el-radio-group v-model="selectedGenre" @change="handleFilter">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button
            v-for="genre in filterOptions.genres"
            :key="genre"
            :label="genre"
          />
        </el-radio-group>
      </div>

      <div class="filter-item">
        <span class="filter-label">年份：</span>
        <el-radio-group v-model="selectedYear" @change="handleFilter">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button
            v-for="year in filterOptions.years"
            :key="year"
            :label="year.split('-')[0]"
            :title="year.split('-')[0]"
          />
        </el-radio-group>
      </div>
    </div>

    <!-- 电影列表 -->
    <div class="movie-section">
      <div v-if="movies.length === 0" class="empty-tip">
      {{ searchStatusText }}
    </div>
    <el-row :gutter="20">
      <el-col 
        v-for="movie in movies" 
        :key="movie.id" 
        :xs="12" 
        :sm="8" 
        :md="6" 
        :lg="6"
      >
        <el-card class="movie-card" shadow="hover">
          <div class="cover-container">
            <img 
              :src="'http://localhost:3000/' + movie.coverPath"
              class="cover-image"
              alt="movie cover"
            />
          </div>
          <div class="movie-info">
            <h3 class="title">{{ movie.title || '暂无标题' }}</h3>
            <div class="meta">
              <el-rate
                v-model="movie.rating"
                disabled
                :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
              />
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      selectedRegion: '',
      selectedGenre: '',
      selectedYear: '',
      searchStatusText: '',
      filterOptions: {
        regions: [],
        genres: [],
        years: []
      },
      movies: []
    };
  },
  mounted() {
    this.fetchMovies();
    axios.get('http://localhost:3000/filmlist/filter-options')
      .then(response => {
        this.filterOptions = response.data;
      })
      .catch(error => {
        console.error('获取筛选条件失败:', error);
      });
  },
  methods: {
    fetchMovies() {
      axios.get('http://localhost:3000/filmlist', {
        params: {title: this.$route.query.q || null, }
      })
      .then(response => {
        this.movies = response.data;
        this.searchStatusText = this.$route.query.q && response.data.length === 0 
          ? `未找到包含“${this.$route.query.q}”的电影` 
          : '当前分类下暂无电影';
      })
    },
    handleFilter() {
      const params = {
        title: this.$route.query.q || null,  // 保持搜索词
        region: this.selectedRegion || null,
        genre: this.selectedGenre || null,
        year: this.selectedYear ? parseInt(this.selectedYear) : null
      };

      axios.get('http://localhost:3000/filmlist', { params })
        .then(response => {
          this.movies = response.data;
        })
        .catch(error => {
          console.error('筛选失败:', error);
        });
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.film-category {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.filter-container {
  background-color: rgba(255, 255, 255, 0.15);
  width: 70%;
  padding: 18px 25px;
  border-radius: 10px;
  margin-bottom: 25px;
  backdrop-filter: blur(5px);
}

.filter-item {
  margin-bottom: 18px;
  display: flex;
  align-items: center;
}

.filter-label {
  margin-right: 15px;
  font-weight: 600;
  color: #e0e0e0;
  min-width: 45px;
}

.el-radio-button__inner {
  background-color: transparent !important;
  color: #f0f0f0;
  border-color: rgba(255, 255, 255, 0.3) !important;
  transition: all 0.3s ease;
}

.el-radio-button__inner:hover {
  color: #409EFF;
  border-color: #409EFF !important;
}

.el-radio-button__original-radio:checked + .el-radio-button__inner {
  background-color: rgba(64, 158, 255, 0.8) !important;
  border-color: #409EFF !important;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.3) !important;
}
/* 电影列表样式 */
.movie-section {
  width: 85%;
  margin: 0 auto;
  padding: 30px 0;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 12px;
  backdrop-filter: blur(8px);
}

.cover-container {
  height: 320px;
  overflow: hidden;
  border-radius: 6px 6px 0 0;
}

.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.movie-card:hover .cover-image {
  transform: scale(1.05);
}

.movie-info {
  padding: 20px;
  background: linear-gradient(180deg, #f8f9fa 0%, #e9ecef 100%);
}
/* 搜索空白 */
.empty-tip {
  color: #ffffff;
  font-size: 18px;
  text-align: center;
  padding: 40px;
  background: rgba(255,255,255,0.1);
  border-radius: 8px;
}
.title {
  font-size: 17px;
  color: #2c3e50;
  margin-bottom: 12px;
  font-weight: 600;
}

.meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.year {
  color: #6c757d;
  font-size: 14px;
  font-family: monospace;
}
</style>